<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns:yui="http://yuilibrary.com/rdf/1.0/yui.rdf#">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<title>API: collection   array-extras.js  (YUI Library)</title>

	<link rel="stylesheet" type="text/css" href="assets/reset-fonts-grids-min.css" />
	<link rel="stylesheet" type="text/css" href="assets/api.css" />

    <script type="text/javascript" src="assets/api-js"></script>
    <script type="text/javascript" src="assets/ac-js"></script>
</head>

<body id="yahoo-com">

<div id="doc3" class="yui-t2">
	<div id="hd">
        <h1><a href="http://developer.yahoo.com/yui/" title="Yahoo! UI Library">Yahoo! UI Library</a></h1>
        <h3>collection&nbsp; <span class="subtitle">3.0.0b1</span></h3>
        <a href="./index.html" title="Yahoo! UI Library">Yahoo! UI Library</a> 
            &gt; <a href="./module_collection.html" title="collection">collection</a>
                
                 &gt; array-extras.js (source view) 
        <form onsubmit="return false">
            <div id="propertysearch">
                Search: <input autocomplete="off" id="searchinput" />
                <div id="searchresults">
                    &nbsp;
                </div>
            </div>
        </form>
	</div>

	<div id="bd">
		<div id="yui-main">
			<div class="yui-b">
            <form action="#" name="yui-classopts-form" method="get" id="yui-classopts-form">
                <fieldset>
                    <legend>Filters</legend>
                <span class="classopts"><input type="checkbox" name="show_private" id="show_private" /> <label for="show_private">Show Private</label></span>
                <span class="classopts"><input type="checkbox" name="show_protected" id="show_protected" /> <label for="show_protected">Show Protected</label></span>
                <span class="classopts"><input type="checkbox" name="show_deprecated" id="show_deprecated" /> <label for="show_deprecated">Show Deprecated</label></span>
                </fieldset>
            </form>

                    <div id="srcout">
                        <style>
                            #doc3 .classopts { display:none; }
                        </style>
<div class="highlight" ><pre><span class="c">/**</span>
<span class="c"> * Collection utilities beyond what is provided in the YUI core</span>
<span class="c"> * @module collection</span>
<span class="c"> */</span>

<span class="k">var</span> <span class="nx">L</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Lang</span><span class="o">,</span> <span class="nx">Native</span> <span class="o">=</span> <span class="nb">Array</span><span class="o">.</span><span class="nx">prototype</span><span class="o">,</span> <span class="nx">A</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">;</span>

<span class="c">/**</span>
<span class="c"> * Adds the following array utilities to the YUI instance</span>
<span class="c"> * (Y.Array).  This is in addition to the methods provided</span>
<span class="c"> * in the core.</span>
<span class="c"> * @class YUI~array~extras</span>
<span class="c"> */</span>

<span class="c">/**</span>
<span class="c"> * Returns the index of the last item in the array</span>
<span class="c"> * that contains the specified value, -1 if the</span>
<span class="c"> * value isn&#39;t found.</span>
<span class="c"> * method Array.lastIndexOf</span>
<span class="c"> * @static</span>
<span class="c"> * @param a {Array} the array to search</span>
<span class="c"> * @param val the value to search for</span>
<span class="c"> * @return {int} the index of hte item that contains the value or -1</span>
<span class="c"> */</span>
<span class="nx">A</span><span class="o">.</span><span class="nx">lastIndexOf</span> <span class="o">=</span> <span class="o">(</span><span class="nx">Native</span><span class="o">.</span><span class="nx">lastIndexOf</span><span class="o">)</span> <span class="o">?</span>
    <span class="k">function</span><span class="o">(</span><span class="nx">a</span> <span class="o">,</span><span class="nx">val</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">return</span> <span class="nx">a</span><span class="o">.</span><span class="nx">lastIndexOf</span><span class="o">(</span><span class="nx">val</span><span class="o">);</span>    
    <span class="o">}</span> <span class="o">:</span>
    <span class="k">function</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">val</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">for</span> <span class="o">(</span><span class="k">var</span> <span class="nx">i</span><span class="o">=</span><span class="nx">a</span><span class="o">.</span><span class="nx">length</span><span class="o">-</span><span class="m">1</span><span class="o">;</span> <span class="nx">i</span><span class="o">&gt;=</span><span class="m">0</span><span class="o">;</span> <span class="nx">i</span><span class="o">=</span><span class="nx">i</span><span class="o">-</span><span class="m">1</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">if</span> <span class="o">(</span><span class="nx">a</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">===</span> <span class="nx">val</span><span class="o">)</span> <span class="o">{</span>
                <span class="k">break</span><span class="o">;</span>
            <span class="o">}</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="nx">i</span><span class="o">;</span>
    <span class="o">};</span>

<span class="c">/**</span>
<span class="c"> * Returns a copy of the array with the duplicate entries removed</span>
<span class="c"> * @method Array.unique</span>
<span class="c"> * @static</span>
<span class="c"> * @param a {Array} the array to find the subset of uniques for</span>
<span class="c"> * @param sort {bool} flag to denote if the array is sorted or not. Defaults to false, the more general operation</span>
<span class="c"> * @return {Array} a copy of the array with duplicate entries removed</span>
<span class="c"> */</span>
<span class="nx">A</span><span class="o">.</span><span class="nx">unique</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">sort</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">var</span> <span class="nx">s</span> <span class="o">=</span> <span class="nx">L</span><span class="o">.</span><span class="nx">isValue</span><span class="o">(</span><span class="nx">sort</span><span class="o">)</span> <span class="o">?</span> <span class="nx">sort</span> <span class="o">:</span> <span class="kc">false</span><span class="o">,</span>
        <span class="nx">b</span> <span class="o">=</span> <span class="nx">a</span><span class="o">.</span><span class="nx">slice</span><span class="o">(),</span> <span class="nx">i</span> <span class="o">=</span> <span class="m">0</span><span class="o">,</span> <span class="nx">n</span> <span class="o">=</span> <span class="o">-</span><span class="m">1</span><span class="o">,</span> <span class="nx">item</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
    <span class="k">if</span> <span class="o">(</span><span class="nx">s</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">while</span> <span class="o">(</span><span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">b</span><span class="o">.</span><span class="nx">length</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">if</span> <span class="o">(</span><span class="nx">b</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">===</span> <span class="nx">item</span><span class="o">)</span> <span class="o">{</span>
                <span class="nx">n</span> <span class="o">=</span> <span class="o">(</span><span class="nx">n</span> <span class="o">==</span> <span class="o">-</span><span class="m">1</span> <span class="o">?</span> <span class="nx">i</span> <span class="o">:</span> <span class="nx">n</span><span class="o">);</span>
                <span class="nx">i</span> <span class="o">+=</span> <span class="m">1</span><span class="o">;</span>
            <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="nx">n</span> <span class="o">!==</span> <span class="o">-</span><span class="m">1</span><span class="o">)</span> <span class="o">{</span>
                <span class="nx">b</span><span class="o">.</span><span class="nx">splice</span><span class="o">(</span><span class="nx">n</span><span class="o">,</span> <span class="nx">i</span><span class="o">-</span><span class="nx">n</span><span class="o">);</span>
                <span class="nx">i</span> <span class="o">=</span> <span class="nx">n</span><span class="o">;</span>                
                <span class="nx">n</span> <span class="o">=</span> <span class="o">-</span><span class="m">1</span><span class="o">;</span>
            <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                <span class="nx">item</span> <span class="o">=</span> <span class="nx">b</span><span class="o">[</span><span class="nx">i</span><span class="o">];</span>
                <span class="nx">i</span> <span class="o">+=</span> <span class="m">1</span><span class="o">;</span>
            <span class="o">}</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="nx">b</span><span class="o">;</span>
    <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
        <span class="k">while</span> <span class="o">(</span><span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">b</span><span class="o">.</span><span class="nx">length</span><span class="o">)</span> <span class="o">{</span>
            <span class="nx">item</span> <span class="o">=</span> <span class="nx">b</span><span class="o">[</span><span class="nx">i</span><span class="o">];</span>
            <span class="k">while</span> <span class="o">((</span><span class="nx">n</span> <span class="o">=</span> <span class="nx">b</span><span class="o">.</span><span class="nx">lastIndexOf</span><span class="o">(</span><span class="nx">item</span><span class="o">))</span> <span class="o">!==</span> <span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
                <span class="nx">b</span><span class="o">.</span><span class="nx">splice</span><span class="o">(</span><span class="nx">n</span><span class="o">,</span> <span class="m">1</span><span class="o">);</span>
            <span class="o">}</span>
            <span class="nx">i</span> <span class="o">+=</span> <span class="m">1</span><span class="o">;</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="nx">b</span><span class="o">;</span>
    <span class="o">}</span>
<span class="o">};</span>

<span class="c">/**</span>
<span class="c">* Executes the supplied function on each item in the array.</span>
<span class="c">* Returns a new array containing the items that the supplied</span>
<span class="c">* function returned true for.</span>
<span class="c">* @method Array.filter</span>
<span class="c">* @param a {Array} the array to iterate</span>
<span class="c">* @param f {Function} the function to execute on each item</span>
<span class="c">* @param o Optional context object</span>
<span class="c">* @static</span>
<span class="c">* @return {Array} The items on which the supplied function</span>
<span class="c">* returned true. If no items matched an empty array is </span>
<span class="c">* returned.</span>
<span class="c">*/</span>
<span class="nx">A</span><span class="o">.</span><span class="nx">filter</span> <span class="o">=</span> <span class="o">(</span><span class="nx">Native</span><span class="o">.</span><span class="nx">filter</span><span class="o">)</span> <span class="o">?</span>
    <span class="k">function</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">return</span> <span class="nx">Native</span><span class="o">.</span><span class="nx">filter</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">);</span>
    <span class="o">}</span> <span class="o">:</span>
    <span class="k">function</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">var</span> <span class="nx">results</span> <span class="o">=</span> <span class="o">[];</span>
        <span class="nx">A</span><span class="o">.</span><span class="nx">each</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="k">function</span><span class="o">(</span><span class="nx">item</span><span class="o">,</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">a</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">if</span> <span class="o">(</span><span class="nx">f</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">item</span><span class="o">,</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">a</span><span class="o">))</span> <span class="o">{</span>
                <span class="nx">results</span><span class="o">.</span><span class="nx">push</span><span class="o">(</span><span class="nx">item</span><span class="o">);</span>
            <span class="o">}</span>
        <span class="o">});</span>

        <span class="k">return</span> <span class="nx">results</span><span class="o">;</span>
    <span class="o">};</span>

<span class="c">/**</span>
<span class="c">* The inverse of filter. Executes the supplied function on each item. </span>
<span class="c">* Returns a new array containing the items that the supplied</span>
<span class="c">* function returned *false* for.</span>
<span class="c">* @method Array.reject</span>
<span class="c">* @param a {Array} the array to iterate</span>
<span class="c">* @param f {Function} the function to execute on each item</span>
<span class="c">* @param o Optional context object</span>
<span class="c">* @static</span>
<span class="c">* @return {Array} The items on which the supplied function</span>
<span class="c">* returned false.</span>
<span class="c">*/</span>
<span class="nx">A</span><span class="o">.</span><span class="nx">reject</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">return</span> <span class="nx">A</span><span class="o">.</span><span class="nx">filter</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="k">function</span><span class="o">(</span><span class="nx">item</span><span class="o">,</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">a</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">return</span> <span class="o">!</span><span class="nx">f</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">item</span><span class="o">,</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">a</span><span class="o">);</span>
    <span class="o">});</span>
<span class="o">};</span>

<span class="c">/**</span>
<span class="c">* Executes the supplied function on each item in the array.</span>
<span class="c">* @method Array.every</span>
<span class="c">* @param a {Array} the array to iterate</span>
<span class="c">* @param f {Function} the function to execute on each item</span>
<span class="c">* @param o Optional context object</span>
<span class="c">* @static</span>
<span class="c">* @return {boolean} true if every item in the array returns true</span>
<span class="c">* from the supplied function.</span>
<span class="c">*/</span>
<span class="nx">A</span><span class="o">.</span><span class="nx">every</span> <span class="o">=</span> <span class="o">(</span><span class="nx">Native</span><span class="o">.</span><span class="nx">every</span><span class="o">)</span> <span class="o">?</span>
    <span class="k">function</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">return</span> <span class="nx">Native</span><span class="o">.</span><span class="nx">every</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span><span class="nx">f</span><span class="o">,</span><span class="nx">o</span><span class="o">);</span>
    <span class="o">}</span> <span class="o">:</span>
    <span class="k">function</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">var</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">a</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span>
        <span class="k">for</span> <span class="o">(</span><span class="k">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="m">0</span><span class="o">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">l</span><span class="o">;</span> <span class="nx">i</span><span class="o">=</span><span class="nx">i</span><span class="o">+</span><span class="m">1</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">if</span> <span class="o">(!</span><span class="nx">f</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">a</span><span class="o">[</span><span class="nx">i</span><span class="o">],</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">a</span><span class="o">))</span> <span class="o">{</span>
                <span class="k">return</span> <span class="kc">false</span><span class="o">;</span>
            <span class="o">}</span>
        <span class="o">}</span>

        <span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
    <span class="o">};</span>

<span class="c">/**</span>
<span class="c">* Executes the supplied function on each item in the array.</span>
<span class="c">* @method Array.map</span>
<span class="c">* @param a {Array} the array to iterate</span>
<span class="c">* @param f {Function} the function to execute on each item</span>
<span class="c">* @param o Optional context object</span>
<span class="c">* @static</span>
<span class="c">* @return {Array} A new array containing the return value</span>
<span class="c">* of the supplied function for each item in the original</span>
<span class="c">* array.</span>
<span class="c">*/</span>
<span class="nx">A</span><span class="o">.</span><span class="nx">map</span> <span class="o">=</span> <span class="o">(</span><span class="nx">Native</span><span class="o">.</span><span class="nx">map</span><span class="o">)</span> <span class="o">?</span> 
    <span class="k">function</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">return</span> <span class="nx">Native</span><span class="o">.</span><span class="nx">map</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">);</span>
    <span class="o">}</span> <span class="o">:</span>
    <span class="k">function</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">var</span> <span class="nx">results</span> <span class="o">=</span> <span class="o">[];</span>
        <span class="nx">A</span><span class="o">.</span><span class="nx">each</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="k">function</span><span class="o">(</span><span class="nx">item</span><span class="o">,</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">a</span><span class="o">)</span> <span class="o">{</span>
            <span class="nx">results</span><span class="o">.</span><span class="nx">push</span><span class="o">(</span><span class="nx">f</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">item</span><span class="o">,</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">a</span><span class="o">));</span>
        <span class="o">});</span>
        <span class="k">return</span> <span class="nx">results</span><span class="o">;</span>
    <span class="o">};</span>


<span class="c">/**</span>
<span class="c">* Executes the supplied function on each item in the array.</span>
<span class="c">* Reduce &quot;folds&quot; the array into a single value.</span>
<span class="c">* @method Array.reduce</span>
<span class="c">* @param a {Array} the array to iterate</span>
<span class="c">* @param init The initial value to start from</span>
<span class="c">* @param f {Function} the function to execute on each item. It</span>
<span class="c">* is responsible for returning the updated value of the</span>
<span class="c">* computation.</span>
<span class="c">* @param o Optional context object</span>
<span class="c">* @static</span>
<span class="c">* @return A value that results from iteratively applying the</span>
<span class="c">* supplied function to each element in the array.</span>
<span class="c">*/</span>
<span class="nx">A</span><span class="o">.</span><span class="nx">reduce</span> <span class="o">=</span> <span class="o">(</span><span class="nx">Native</span><span class="o">.</span><span class="nx">reduce</span><span class="o">)</span> <span class="o">?</span>
    <span class="k">function</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">init</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
        <span class="c">//Firefox&#39;s Array.reduce does not allow inclusion of a</span>
<span class="c"></span>        <span class="c">//  thisObject, so we need to implement it manually</span>
<span class="c"></span>        <span class="k">return</span> <span class="nx">Native</span><span class="o">.</span><span class="nx">reduce</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="k">function</span><span class="o">(</span><span class="nx">init</span><span class="o">,</span> <span class="nx">item</span><span class="o">,</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">a</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">return</span> <span class="nx">f</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">init</span><span class="o">,</span> <span class="nx">item</span><span class="o">,</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">a</span><span class="o">);</span>
        <span class="o">},</span> <span class="nx">init</span><span class="o">);</span>
    <span class="o">}</span> <span class="o">:</span>
    <span class="k">function</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">init</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">var</span> <span class="nx">r</span> <span class="o">=</span> <span class="nx">init</span><span class="o">;</span>
        <span class="nx">A</span><span class="o">.</span><span class="nx">each</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="k">function</span> <span class="o">(</span><span class="nx">item</span><span class="o">,</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">a</span><span class="o">)</span> <span class="o">{</span>
            <span class="nx">r</span> <span class="o">=</span> <span class="nx">f</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">r</span><span class="o">,</span> <span class="nx">item</span><span class="o">,</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">a</span><span class="o">);</span>
        <span class="o">});</span>
        <span class="k">return</span> <span class="nx">r</span><span class="o">;</span>
    <span class="o">};</span>


<span class="c">/**</span>
<span class="c">* Executes the supplied function on each item in the array,</span>
<span class="c">* searching for the first item that matches the supplied</span>
<span class="c">* function.</span>
<span class="c">* @method Array.find</span>
<span class="c">* @param a {Array} the array to search</span>
<span class="c">* @param f {Function} the function to execute on each item. </span>
<span class="c">* Iteration is stopped as soon as this function returns true</span>
<span class="c">* on an item.</span>
<span class="c">* @param o Optional context object</span>
<span class="c">* @static</span>
<span class="c">* @return {object} the first item that the supplied function</span>
<span class="c">* returns true for, or null if it never returns true</span>
<span class="c">*/</span>
<span class="nx">A</span><span class="o">.</span><span class="nx">find</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">var</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">a</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span>
    <span class="k">for</span><span class="o">(</span><span class="k">var</span> <span class="nx">i</span><span class="o">=</span><span class="m">0</span><span class="o">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">l</span><span class="o">;</span> <span class="nx">i</span><span class="o">++)</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="nx">f</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">a</span><span class="o">[</span><span class="nx">i</span><span class="o">],</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">a</span><span class="o">))</span> <span class="o">{</span>
            <span class="k">return</span> <span class="nx">a</span><span class="o">[</span><span class="nx">i</span><span class="o">];</span>
        <span class="o">}</span>
    <span class="o">}</span>
    <span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">};</span>

<span class="c">/**</span>
<span class="c">* Iterates over an array, returning a new array of all the elements</span>
<span class="c">* that match the supplied regular expression</span>
<span class="c">* @method Array.grep</span>
<span class="c">* @param a {Array} a collection to iterate over</span>
<span class="c">* @param pattern {RegExp} The regular expression to test against </span>
<span class="c">* each item</span>
<span class="c">* @static</span>
<span class="c">* @return {Array} All the items in the collection that </span>
<span class="c">* produce a match against the supplied regular expression. </span>
<span class="c">* If no items match, an empty array is returned.</span>
<span class="c">*/</span>
<span class="nx">A</span><span class="o">.</span><span class="nx">grep</span> <span class="o">=</span> <span class="k">function</span> <span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">pattern</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">return</span> <span class="nx">A</span><span class="o">.</span><span class="nx">filter</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="k">function</span> <span class="o">(</span><span class="nx">item</span><span class="o">,</span> <span class="nx">index</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">return</span> <span class="nx">pattern</span><span class="o">.</span><span class="nx">test</span><span class="o">(</span><span class="nx">item</span><span class="o">);</span>
    <span class="o">});</span>
<span class="o">};</span>


<span class="c">/**</span>
<span class="c">* Partitions an array into two new arrays, one with the items</span>
<span class="c">* that match the supplied function, and one with the items that</span>
<span class="c">* do not.</span>
<span class="c">* @method Array.partition</span>
<span class="c">* @param a {Array} a collection to iterate over</span>
<span class="c">* @paran f {Function} a function that will receive each item </span>
<span class="c">* in the collection and its index.</span>
<span class="c">* @param o Optional execution context of f.</span>
<span class="c">* @static</span>
<span class="c">* @return An object with two members, &#39;matches&#39; and &#39;rejects&#39;,</span>
<span class="c">* that are arrays containing the items that were selected or </span>
<span class="c">* rejected by the test function (or an empty array).</span>
<span class="c">*/</span>
<span class="nx">A</span><span class="o">.</span><span class="nx">partition</span> <span class="o">=</span> <span class="k">function</span> <span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">var</span> <span class="nx">results</span> <span class="o">=</span> <span class="o">{</span><span class="nx">matches</span><span class="o">:</span> <span class="o">[],</span> <span class="nx">rejects</span><span class="o">:</span> <span class="o">[]};</span>
    <span class="nx">A</span><span class="o">.</span><span class="nx">each</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="k">function</span> <span class="o">(</span><span class="nx">item</span><span class="o">,</span> <span class="nx">index</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">var</span> <span class="nx">set</span> <span class="o">=</span> <span class="nx">f</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">item</span><span class="o">,</span> <span class="nx">index</span><span class="o">,</span> <span class="nx">a</span><span class="o">)</span> <span class="o">?</span> <span class="nx">results</span><span class="o">.</span><span class="nx">matches</span> <span class="o">:</span> <span class="nx">results</span><span class="o">.</span><span class="nx">rejects</span><span class="o">;</span>
        <span class="nx">set</span><span class="o">.</span><span class="nx">push</span><span class="o">(</span><span class="nx">item</span><span class="o">);</span>
    <span class="o">});</span>
    <span class="k">return</span> <span class="nx">results</span><span class="o">;</span>
<span class="o">};</span>

<span class="c">/**</span>
<span class="c">* Creates an array of arrays by pairing the corresponding</span>
<span class="c">* elements of two arrays together into a new array.</span>
<span class="c">* @method Array.zip</span>
<span class="c">* @param a {Array} a collection to iterate over</span>
<span class="c">* @param a2 {Array} another collection whose members will be </span>
<span class="c">* paired with members of the first parameter</span>
<span class="c">* @static</span>
<span class="c">* @return An array of arrays formed by pairing each element </span>
<span class="c">* of the first collection with an item in the second collection </span>
<span class="c">* having the corresponding index.</span>
<span class="c">*/</span>
<span class="nx">A</span><span class="o">.</span><span class="nx">zip</span> <span class="o">=</span> <span class="k">function</span> <span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="nx">a2</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">var</span> <span class="nx">results</span> <span class="o">=</span> <span class="o">[];</span>
    <span class="nx">A</span><span class="o">.</span><span class="nx">each</span><span class="o">(</span><span class="nx">a</span><span class="o">,</span> <span class="k">function</span> <span class="o">(</span><span class="nx">item</span><span class="o">,</span> <span class="nx">index</span><span class="o">)</span> <span class="o">{</span>
        <span class="nx">results</span><span class="o">.</span><span class="nx">push</span><span class="o">([</span><span class="nx">item</span><span class="o">,</span> <span class="nx">a2</span><span class="o">[</span><span class="nx">index</span><span class="o">]]);</span>
    <span class="o">});</span>
    <span class="k">return</span> <span class="nx">results</span><span class="o">;</span>
<span class="o">};</span>
</pre></div>
                    </div>
			</div>
		</div>
		<div class="yui-b">
            <div class="nav">

                    <div id="moduleList" class="module">
                        <h4>Modules</h4>
                        <ul class="content">
                                <li class=""><a href="module_anim.html" title="anim">anim</a></li>
                                <li class=""><a href="module_attribute.html" title="attribute">attribute</a></li>
                                <li class=""><a href="module_base.html" title="base">base</a></li>
                                <li class=""><a href="module_cache.html" title="cache">cache</a></li>
                                <li class=""><a href="module_classnamemanager.html" title="classnamemanager">classnamemanager</a></li>
                                <li class="selected"><a href="module_collection.html" title="collection">collection</a></li>
                                <li class=""><a href="module_console.html" title="console">console</a></li>
                                <li class=""><a href="module_console-filters.html" title="console-filters">console-filters</a></li>
                                <li class=""><a href="module_cookie.html" title="cookie">cookie</a></li>
                                <li class=""><a href="module_dataschema.html" title="dataschema">dataschema</a></li>
                                <li class=""><a href="module_datasource.html" title="datasource">datasource</a></li>
                                <li class=""><a href="module_datatype.html" title="datatype">datatype</a></li>
                                <li class=""><a href="module_dd.html" title="dd">dd</a></li>
                                <li class=""><a href="module_dom.html" title="dom">dom</a></li>
                                <li class=""><a href="module_dump.html" title="dump">dump</a></li>
                                <li class=""><a href="module_event.html" title="event">event</a></li>
                                <li class=""><a href="module_event-custom.html" title="event-custom">event-custom</a></li>
                                <li class=""><a href="module_event-simulate.html" title="event-simulate">event-simulate</a></li>
                                <li class=""><a href="module_history.html" title="history">history</a></li>
                                <li class=""><a href="module_imageloader.html" title="imageloader">imageloader</a></li>
                                <li class=""><a href="module_io.html" title="io">io</a></li>
                                <li class=""><a href="module_json.html" title="json">json</a></li>
                                <li class=""><a href="module_node.html" title="node">node</a></li>
                                <li class=""><a href="module_node-focusmanager.html" title="node-focusmanager">node-focusmanager</a></li>
                                <li class=""><a href="module_node-menunav.html" title="node-menunav">node-menunav</a></li>
                                <li class=""><a href="module_oop.html" title="oop">oop</a></li>
                                <li class=""><a href="module_overlay.html" title="overlay">overlay</a></li>
                                <li class=""><a href="module_plugin.html" title="plugin">plugin</a></li>
                                <li class=""><a href="module_profiler.html" title="profiler">profiler</a></li>
                                <li class=""><a href="module_queue.html" title="queue">queue</a></li>
                                <li class=""><a href="module_slider.html" title="slider">slider</a></li>
                                <li class=""><a href="module_stylesheet.html" title="stylesheet">stylesheet</a></li>
                                <li class=""><a href="module_substitute.html" title="substitute">substitute</a></li>
                                <li class=""><a href="module_test.html" title="test">test</a></li>
                                <li class=""><a href="module_widget.html" title="widget">widget</a></li>
                                <li class=""><a href="module_widget-position.html" title="widget-position">widget-position</a></li>
                                <li class=""><a href="module_widget-position-ext.html" title="widget-position-ext">widget-position-ext</a></li>
                                <li class=""><a href="module_widget-stack.html" title="widget-stack">widget-stack</a></li>
                                <li class=""><a href="module_widget-stdmod.html" title="widget-stdmod">widget-stdmod</a></li>
                                <li class=""><a href="module_yui.html" title="yui">yui</a></li>
                        </ul>
                    </div>

                    <div id="classList" class="module">
                        <h4>Classes</h4>
                        <ul class="content">
                                <li class=""><a href="YUI~array~extras.html" title="YUI~array~extras">YUI~array~extras</a></li>
                        </ul>
                    </div>

                    <div id="fileList" class="module">
                        <h4>Files</h4>
                        <ul class="content">        
                                <li class="selected"><a href="array-extras.js.html" title="array-extras.js">array-extras.js</a></li>
                        </ul>
                    </div>





            </div>
		</div>
	</div>
	<div id="ft">
        <hr />
        Copyright &copy; 2009 Yahoo! Inc. All rights reserved.
	</div>
</div>
<script type="text/javascript">
    ALL_YUI_PROPS = [{"access": "", "host": "YUI~array~extras", "name": "Array.every", "url": "YUI~array~extras.html#method_Array.every", "type": "method"}, {"access": "", "host": "YUI~array~extras", "name": "Array.filter", "url": "YUI~array~extras.html#method_Array.filter", "type": "method"}, {"access": "", "host": "YUI~array~extras", "name": "Array.find", "url": "YUI~array~extras.html#method_Array.find", "type": "method"}, {"access": "", "host": "YUI~array~extras", "name": "Array.grep", "url": "YUI~array~extras.html#method_Array.grep", "type": "method"}, {"access": "", "host": "YUI~array~extras", "name": "Array.map", "url": "YUI~array~extras.html#method_Array.map", "type": "method"}, {"access": "", "host": "YUI~array~extras", "name": "Array.partition", "url": "YUI~array~extras.html#method_Array.partition", "type": "method"}, {"access": "", "host": "YUI~array~extras", "name": "Array.reduce", "url": "YUI~array~extras.html#method_Array.reduce", "type": "method"}, {"access": "", "host": "YUI~array~extras", "name": "Array.reject", "url": "YUI~array~extras.html#method_Array.reject", "type": "method"}, {"access": "", "host": "YUI~array~extras", "name": "Array.unique", "url": "YUI~array~extras.html#method_Array.unique", "type": "method"}, {"access": "", "host": "YUI~array~extras", "name": "Array.zip", "url": "YUI~array~extras.html#method_Array.zip", "type": "method"}, {"access": "", "host": "YUI~array~extras", "name": "lastIndexOf", "url": "YUI~array~extras.html#property_lastIndexOf", "type": "property"}];
</script>
</body>
</html>
